package thing

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"iot-base/auth/api/logic"
	Log "iot-base/auth/logger"
	types2 "iot-base/auth/types"
)

func QueryHis(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	var req types2.ReqQueryHis
	if err := c.ShouldBind(&req); err != nil {
		fmt.Println(err)
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	hisLogic := logic.NewHisLogic()
	total, err := hisLogic.QueryHisTotal(req.StationId, req.Classify, req.StartTime, req.EndTime)
	if err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.DBError, "")
		return
	}
	result, err := hisLogic.QueryHis(req.StationId, req.Page, req.Size, req.Classify, req.StartTime, req.EndTime)
	if err != nil {
		Log.Log.Errorf("[%s] [%v] err:=%v", requestId, uid, err)
		resp.ErrorCode(types2.DBError, "")
		return
	}
	resp.Success(gin.H{"list": result, "total": total})
}
